package com.cloudwise.gproject.controller;

import com.cloudwise.gproject.entity.Custom;
import com.cloudwise.gproject.entity.PotentialCustomer;
import com.cloudwise.gproject.entity.User;
import com.cloudwise.gproject.service.CustomService;
import com.cloudwise.gproject.service.PotentialCustomerService;
import com.cloudwise.gproject.util.Result;
import com.cloudwise.gproject.util.ReturnStatus;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * (Custom)表控制层
 *
 * @author makejava
 * @since 2021-03-20 08:35:59
 */
@Controller
@RequestMapping("custom")
public class CustomController {
    /**
     * 服务对象
     */
    @Resource
    private CustomService customService;
    /**
     * 潜在客户
     */
    @Resource
    private PotentialCustomerService potentialCustomerService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ResponseBody
    @RequestMapping("selectOne")
    public Result selectOne(Integer id) {
        return new Result(ReturnStatus.SUCCESS.getCode(),ReturnStatus.SUCCESS.getMessage(),
                1,this.customService.queryById(id));
    }

    /** 
     * @create by: 卫子健
     * @description: 跳转到部门管理页面dept.html 
     * @create time: 2021/3/19 19:45
     * @param  
     * @return  
     */
    @RequestMapping("toCustom")
    public String toDept(){
        return "back/custom";
    }
    /** 
     * @create by: 卫子健
     * @description: 分页条件查询所有客户列表 
     * @create time: 2021/3/19 20:35
     * @param page
     * @param limit
     * @param custom 
     * @return com.cloudwise.trademark.util.Result 
     */
    @ResponseBody
    @RequestMapping("queryAll")
    public Result queryAll(Integer page, Integer limit, Custom custom, HttpServletRequest request){
        HttpSession session = request.getSession();
        custom.setCustomId((Integer)session.getAttribute("ScustomId"));
        PageInfo pageInfo = customService.queryAll(page, limit, custom);
        session.removeAttribute("ScustomId");
        return new Result(ReturnStatus.SUCCESS.getCode(),ReturnStatus.SUCCESS.getMessage(),
                pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 为progress.html的小table（customTable）获取数据
     * @param page
     * @param limit
     * @param custom
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("queryAll2")
    public Result queryAll2(Integer page, Integer limit, Custom custom, HttpServletRequest request){
        HttpSession session = request.getSession();
        Object customId = session.getAttribute("kcustomId");
        custom.setCustomId((Integer) customId);
        PageInfo pageInfo = customService.queryAll(page, limit, custom);
        session.removeAttribute("kcustomId");
        return new Result(ReturnStatus.SUCCESS.getCode(),ReturnStatus.SUCCESS.getMessage(),
                pageInfo.getTotal(),pageInfo.getList());
    }
    /** 
     * @create by: 卫子健
     * @description: 根据id删除部门 
     * @create time: 2021/3/19 20:57
     * @param  
     * @return  
     */
    @ResponseBody
    @RequestMapping("deleteById")
    public Result deleteById(Integer customId){
        boolean b = customService.deleteById(customId);
        return new Result(ReturnStatus.SUCCESS.getCode(),ReturnStatus.SUCCESS.getMessage(),
                0,b);
    }
    /** 
     * @create by: 卫子健
     * @description: 更新单个数据 
     * @create time: 2021/3/19 21:44
     * @param  
     * @return  
     */
    @ResponseBody
    @PutMapping("update")
    public Result update(Custom custom, HttpSession session){
        //获取当前用户的用户名即为 执行了更新操作的人
        User userInfo = (User)session.getAttribute("userInfo");
        custom.setUpdateBy(userInfo.getUserName());
        Custom update = customService.update(custom);
        return new Result(ReturnStatus.SUCCESS.getCode(),ReturnStatus.SUCCESS.getMessage(),
                0,update);
    }

    @ResponseBody
    @PostMapping("insert")
    public Result insert(Custom custom, HttpSession session){
        User userInfo = (User)session.getAttribute("userInfo");
        custom.setCreateBy(userInfo.getUserName());
        custom.setUpdateBy(userInfo.getUserName());
        Custom insert = customService.insert(custom);
        return new Result(ReturnStatus.SUCCESS.getCode(),ReturnStatus.SUCCESS.getMessage(),
                0,insert);
    }

    @ResponseBody
    @RequestMapping("deleteByIds")
    public Result deleteByIds(String customIds){
        int i = customService.deptIds(customIds);
        return new Result(ReturnStatus.SUCCESS.getCode(),ReturnStatus.SUCCESS.getMessage(),
                0,i);
    }

    /** 
     * @create by: 卫子健
     * @description: 添加潜在客户 
     * @create time: 2021/3/28 12:38
     * @param  
     * @return  
     */
    @ResponseBody
    @RequestMapping("addPotentialCustomer")
    public Result addPotentialCustomer(PotentialCustomer custom){
        String name = custom.getName();
        Integer integer = potentialCustomerService.queryByName(name);
        if (integer<=2){

            potentialCustomerService.insert(custom);

            return new Result(ReturnStatus.SUCCESS.getCode(),ReturnStatus.SUCCESS.getMessage(),
                    0,null);
        }
        return new Result(ReturnStatus.SUCCESS.getCode(),ReturnStatus.SUCCESS.getMessage(),
                0,null);

    }
}
